const MyPromise = require('./myPromise');
const { resolve } = require('./myPromise');

let promise = new MyPromise((resolve, reject) => {
  
  // setTimeout(() => {
  //   resolve('成功')
  // }, 3000);
  resolve('成功')
})

// promise.then(value => {
//   console.log('then success1 ', value)
// }, reason => {
//   console.log('then fail1 ', reason)
// })

// promise.then(value => {
//   console.log('then success2 ', value)
// }, reason => {
//   console.log('then fail2 ', reason)
// })

function other () {
  return new MyPromise((resolve, reject) => {
    resolve('other')
  })
}

function delay(time = 1000) {
  return new MyPromise(resolve => {
    setTimeout(() => {
      resolve(`delay ${time}`)
    }, time);
  })
}

function throwException(time) {
  return new MyPromise((resolve, rejcet) => {
    setTimeout(() => {
      rejcet(`throw exception ${time}`)
    }, time);
  })
}

promise.then(value => {
  console.log('then success3 ', value)
  return other();
}, reason => {
  console.log('then fail3 ', reason)
}).then((value) => {
  console.log('then.then ', value);
}).finally(value => {
  console.log('finally')
});

MyPromise.all([delay(500), '1', '2', delay(2000)]).then(value => console.log(`all`, value))

MyPromise.race([delay(100), throwException(50), 1, delay(500)]).then(value => console.log('race ', value))